/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import paw.eltabique.domain.Mensaje;
import paw.eltabique.domain.Perfil;
import paw.eltabique.domain.Usuario;
import paw.eltabique.service.interfaces.MensajeFacadeLocal;
import paw.eltabique.service.interfaces.PerfilFacadeLocal;
import paw.eltabique.service.interfaces.UsuarioFacadeLocal;

/**
 *
 * @author usuario_local
 */
@WebServlet(name="test", urlPatterns={"/test"})
public class test extends HttpServlet {
    @EJB
    private MensajeFacadeLocal mensajeFacade;
    @EJB
    private UsuarioFacadeLocal usuarioFacade;
    @EJB
    private PerfilFacadeLocal perfilFacade;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet test</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet test at " + request.getContextPath () + "</h1>");

            if (perfilFacade.count() == 0) {

                Perfil prflInvitado = new Perfil();
                Perfil prflAdministrador = new Perfil();
                Perfil prflAutorizado = new Perfil();
                prflAdministrador.setDescripcion("Administrador");
                prflAutorizado.setDescripcion("Autorizado");
                prflInvitado.setDescripcion("Invitado");
                prflAdministrador.setId(new Long(1));
                prflAutorizado.setId(new Long(2));
                prflInvitado.setId(new Long(3));
                perfilFacade.create(prflAdministrador);
                perfilFacade.create(prflAutorizado);
                perfilFacade.create(prflInvitado);

                Usuario usr = new Usuario();
                usr.setLogin("Admin");
                usr.setPassword("Admin");
                usr.setPerfil(prflAdministrador);
                usuarioFacade.create(usr);

                Mensaje msj = new Mensaje();
                msj.setContenido("Test Message");
                msj.setUsuario(usr);
                Date date = new Date();
                msj.setFecha(date);
                mensajeFacade.create(msj);
            }

            out.println("Base de datos inciale");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
